package com.broada.apm.indicator.api.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.broada.apm.dao.ServiceDao;
import com.broada.apm.indicator.api.ServiceService;
import com.broada.apm.model.SSLCertificate;
import com.broada.apm.model.Service;

public class ServiceServiceImpl implements ServiceService {

	@Autowired
	private ServiceDao serviceDao;
	
	@Override
	public void addService(Service service) {
		serviceDao.addService(service);
	}

	@Override
	public void updateService(Service service) {
		serviceDao.updateService(service);
	}

	@Override
	public void deleteService(Service service) {
		serviceDao.deleteService(service);
	}

	@Override
	public Service getServiceById(String serviceId) {
		return serviceDao.getServiceById(serviceId);
	}

	@Override
	public List<Service> getServicesByAppId(String appId) {
		return serviceDao.getServicesByAppId(appId);
	}

	@Override
	public void deleteServiceByAppId(String appId) {
		serviceDao.deleteServiceByAppId(appId);
	}

	@Override
	public List<Service> getChildServiceByClusterId(String clusterId) {
		return serviceDao.getChildServiceByClusterId(clusterId);
	}

	@Override
	public List<Service> getDisServiceByIpAndPort(String ip, int port) {
		Service service = new Service();
		service.setIp(ip);
		service.setPort(port);
		return serviceDao.getDisServiceByIpAndPort(service);
	}

	@Override
	public Service getDisServiceById(String serviceId) {
		return serviceDao.getDisServiceById(serviceId);
	}

	@Override
	public void updateDisService(Service disService) {
		serviceDao.updateDisService(disService);
	}

	@Override
	public List<Service> getDisServiceByIpNum(long ipNum) {
		return serviceDao.getDisServiceByIpNum(ipNum);
	}

	@Override
	public List<Service> getDisServiceByProtocal(Service service, String orderBy, boolean desc) {
		return serviceDao.getDisServiceByProtocal(service, orderBy, desc);
	}

	@Override
	public void addDisService(Service service) {
		serviceDao.addDisService(service);
	}

	@Override
	public synchronized void saveDisService(Service service) {
		Service exist = serviceDao.getDisServiceById(service.getId());
		if (exist == null)
			serviceDao.addDisService(service);
		else {
			if(exist.getAutoDiscoveryProtocol() != service.getAutoDiscoveryProtocol())  {
				Service serv = new Service();
				serv.setId(service.getId());
				serv.setAutoDiscoveryProtocol(service.getAutoDiscoveryProtocol());
				serviceDao.updateServiceLikeDisId(serv);
			}
			service.setFocus(exist.getFocus());
			serviceDao.updateDisService(service);
		}
	}

	@Override
	public List<Service> getAllServicesNotCluster() {
		return serviceDao.getAllServicesNotCluster();
	}

	@Override
	public SSLCertificate getSSLBySericeId(String serviceId) {
		return serviceDao.getSSLByServiceId(serviceId);
	}

	@Override
	public void addSSLCertificate(SSLCertificate sslCertificate) {
		serviceDao.addSSLCertificate(sslCertificate);
		
	}

	@Override
	public void updateSSLCertificate(SSLCertificate sslCertificate) {
		serviceDao.updateSSLCertificate(sslCertificate);
		
	}

	@Override
	public void deleteSSLCertificate(SSLCertificate sslCertificate) {
		serviceDao.deleteSSLCertificate(sslCertificate);
		
	}

	@Override
	public List<Service> getDisServiceByKeyWord(Service service, String keyWord, String orderBy, boolean desc) {
		return serviceDao.getDisServiceByKeyWord(service, keyWord, orderBy, desc);
	}

	@Override
	public List<Service> getDisServiceByCondition(Service service, String orderBy, boolean desc) {
		return serviceDao.getDisServiceByCondition(service, orderBy, desc);
	}

	@Override
	public void deleteDiscoveryService(Service service) {
		serviceDao.deleteDiscoveryService(service);
		
	}

	@Override
	public List<Service> getTopoServiceBySerivceId(String id) {
		return serviceDao.getTopoServiceBySerivceId(id);
	}

	@Override
	public Service getAppServiceByIpAndPort(String appId, String ip, int port) {
		Service service = new Service();
		service.setId(appId + "-");
		service.setIp(ip);
		service.setPort(port);
		return serviceDao.getAppServiceByIpAndPort(service);
	}

	@Override
	public List<Service> getDisServiceFocusOrNot(com.broada.apm.model.Service service, String serviceType, String orderByMysql, boolean desc, int pageSize, int pageIndex, int severity, Date startDate, Date endDate) {
		return serviceDao.getDisServiceFocusOrNot(service, serviceType, orderByMysql, desc, pageSize, pageIndex, severity, startDate, endDate);
	}

	@Override
	public int getServiceCount(int focus) {
		return serviceDao.getServiceCount(focus);
	}

	@Override
	public void updateDisServiceFocus(Service service) {
		serviceDao.updateDisServiceFocus(service);
	}

	@Override
	public List<Service> queryDisServiceByLabelName(String ip) {
		return serviceDao.getDisServiceByLabelName(ip);
	}

	@Override
	public List<String> getIpsByServiceId(String dstId) {
		return serviceDao.getIpsByServiceId(dstId);
	}

	@Override
	public List<String> getIpsByAppId(String appId) {
		return serviceDao.getIpsByAppId(appId);
	}

	@Override
	public void updateDisServiceLua(Service service) {
		serviceDao.updateDisServiceLua(service);
		
	}

	@Override
	public List<Service> getAllDisService() {
		return serviceDao.getAllDisService();
	}
	
	@Override
	public List<Service> getAllFocusedDisService() {
		return serviceDao.getAllFocusedDisService();
	}

	@Override
	public String getClusterServiceByChild(String serviceId) {
		return serviceDao.getClusterServiceByChildId(serviceId);
	}

	@Override
	public void deleteDiscoveryServiceByIds(List<String> serviceIds) {
		serviceDao.deleteDiscoveryServiceByIds(serviceIds);
	}

	@Override
	public List<String> getChildServiceIdsByApplicationId(String applicationId) {
		return serviceDao.getChildrenServiceIds(applicationId);
	}
}
